package com.geekstudy.week13.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class KafkaConsumerService {

    final Logger logger = LoggerFactory.getLogger(KafkaConsumerService.class);

    @KafkaListener(topics = KafkaProducerService.TopicDemo1, groupId= KafkaProducerService.TopicGroup1)
    public void topicTest(ConsumerRecord<String, String> record, Acknowledgment ack, String topic) {
        logger.info("topicTest topic:{} ", topic);
        Optional message = Optional.ofNullable(record.value());
        if(message.isPresent()) {
            Object m = message.get();
            logger.info("topic:{} message:{} ", topic, m);

            if(ack != null) {
                ack.acknowledge();
            }

        }

    }
}
